//
//  MarketViewController.m
//  bitcoin
//
//  Created by  旺链 on 2018/5/23.
//  Copyright © 2018年 比特币. All rights reserved.
//

#import "MarketViewController.h"
#import "MarketChildViewController.h"
#import "SearchViewController.h"


@interface MarketViewController ()

@property (nonatomic, strong) UIView *redView;
@property(nonatomic,strong)NSMutableArray *taps;

@end

@implementation MarketViewController


- (UIButton *)searchBtn
{
    if (!_searchBtn) {
        _searchBtn = [[UIButton alloc]init];
    }
    return _searchBtn;
    
}
-(UIView *)redView {
    if (!_redView) {
        _redView = [[UIView alloc] initWithFrame:CGRectZero];
        _redView.backgroundColor = [UIColor colorWithRed:168.0/255.0 green:20.0/255.0 blue:4/255.0 alpha:1];
    }
    return _redView;
}
- (NSMutableArray*)taps
{
    if (!_taps) {
        _taps = [NSMutableArray array];
        _taps.array = @[@{@"id": @1,@"name": @"比特币",@"logo": @18,@"mark": @"BTC"},@"自选"];
    }
    return _taps;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    if (self.menuViewStyle == WMMenuViewStyleTriangle) {
        [self.view addSubview:self.redView];
    }
    self.view.backgroundColor = [UIColor colorWithRed:46/255.0 green:49/255.0 blue:69/255.0 alpha:1];
//    self.automaticallyCalculatesItemWidths = YES;
    self.menuItemWidth = 75;
    
    [self addRight];
    [self getData];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}
- (void)getData
{
    [LSRequestManager getRequestWithURL:MARKET(@"listCurrency") params:nil success:^(NSDictionary *responseData) {
        
        HandleResponse(
                       [self.taps removeAllObjects];
                       [self.taps addObjectsFromArray:responseData[@"data"]];
                       [self.taps addObject:@"自选"];
                       [self reloadData];
                       
                       self.menuView.backgroundColor = [UIColor colorWithRed:46/255.0 green:49/255.0 blue:69/255.0 alpha:1];
                    
                       ) NSLog(@"11");}
        
    } failure:^(NSError *error) {
        
    }];
}
- (void)addRight
{
    [self.view addSubview:self.searchBtn];
    [self.searchBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.right.equalTo(0);
        make.top.equalTo(iPhoneX?44:20);
        make.width.equalTo(50);
        make.height.equalTo(44);
    }];
    [self.searchBtn setImage:[UIImage imageNamed:@"seacher"] forState:UIControlStateNormal];
    [self.searchBtn addTarget:self action:@selector(searchClick:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)searchClick:(UIButton *)btn
{
    SearchViewController *search = [[SearchViewController alloc]init];
    search.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:search animated:YES];
}
- (NSInteger)numbersOfChildControllersInPageController:(WMPageController *)pageController {
    return self.taps.count;
}

- (NSString *)pageController:(WMPageController *)pageController titleAtIndex:(NSInteger)index {
    
    if (index == self.taps.count-1) {
        
        return self.taps[index];
    }else{
        NSDictionary *dic = self.taps[index];
        return dic[@"mark"];
    }
    
}

- (UIViewController *)pageController:(WMPageController *)pageController viewControllerAtIndex:(NSInteger)index {

    NSInteger type=0;
    if (index == self.taps.count-1) {
        //自选
        type = 100;
    }else{
        NSDictionary *dic = self.taps[index];
        type = [dic[@"id"]integerValue];
    }
    MarketChildViewController * vc = [[MarketChildViewController alloc]init];
    vc.maketType = type;
    vc.isLeft = self.isLeft;
    vc.clickBack = ^(NSDictionary*dic) {
      
        if (self.clickBack) {
            self.clickBack(dic);
        }
    };
    return vc;
}

//- (CGFloat)menuView:(WMMenuView *)menu widthForItemAtIndex:(NSInteger)index {
//    CGFloat width = [super menuView:menu widthForItemAtIndex:index];
//    return width;
//}

- (CGRect)pageController:(WMPageController *)pageController preferredFrameForMenuView:(WMMenuView *)menuView {
    //    if (self.menuViewPosition == WMMenuViewPositionBottom) {
    //        menuView.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1.0];
    //        return CGRectMake(0, self.view.frame.size.height - 44, self.view.frame.size.width, 44);
    //    }
    CGFloat leftMargin = self.showOnNavigationBar ? 50 : 0;
    //CGFloat originY = self.showOnNavigationBar ? 0 : CGRectGetMaxY(self.navigationController.navigationBar.frame);
    
    return CGRectMake(leftMargin, iPhoneX?44:20, self.view.frame.size.width - 2*leftMargin -rightSpace, 44);
}

- (CGRect)pageController:(WMPageController *)pageController preferredFrameForContentView:(WMScrollView *)contentView {
    //    if (self.menuViewPosition == WMMenuViewPositionBottom) {
    //        return CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64 - 44);
    //    }
    CGFloat originY = CGRectGetMaxY([self pageController:pageController preferredFrameForMenuView:self.menuView]);
    if (self.menuViewStyle == WMMenuViewStyleTriangle) {
        originY += self.redView.frame.size.height;
    }
    return CGRectMake(0, originY, self.view.frame.size.width, self.view.frame.size.height - originY);
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
